<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Resumable C++ 20 Coroutines</title>
<link rel="stylesheet" href="../../../../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../../../boost_asio.html" title="Boost.Asio">
<link rel="up" href="../composition.html" title="Composition and Completion Tokens">
<link rel="prev" href="cpp20_coroutines.html" title="C++20 Coroutines Support">
<link rel="next" href="deferred.html" title="Deferred Operations">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../boost.png"></td>
<td align="center"><a href="../../../../../index.html">Home</a></td>
<td align="center"><a href="../../../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="cpp20_coroutines.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../composition.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../boost_asio.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="deferred.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="boost_asio.overview.composition.coro"></a><a class="link" href="coro.html" title="Resumable C++ 20 Coroutines">Resumable C++
        20 Coroutines</a>
</h4></div></div></div>
<div class="note"><table border="0" summary="Note">
<tr>
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../../doc/src/images/note.png"></td>
<th align="left">Note</th>
</tr>
<tr><td align="left" valign="top"><p>
            This is an experimental feature.
          </p></td></tr>
</table></div>
<p>
          The <a class="link" href="../../reference/experimental__coro.html" title="experimental::coro"><code class="computeroutput"><span class="identifier">experimental</span><span class="special">::</span><span class="identifier">coro</span></code></a> class provides support for
          a universal C++20 coroutine. These coroutines can be used as tasks, generators
          and transfomers, depending on their signature.
        </p>
<pre class="programlisting"><span class="identifier">coro</span><span class="special">&lt;</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">string_view</span><span class="special">&gt;</span> <span class="identifier">line_reader</span><span class="special">(</span><span class="identifier">tcp</span><span class="special">::</span><span class="identifier">socket</span> <span class="identifier">stream</span><span class="special">)</span>
<span class="special">{</span>
   <span class="keyword">while</span> <span class="special">(</span><span class="identifier">stream</span><span class="special">.</span><span class="identifier">is_open</span><span class="special">())</span>
   <span class="special">{</span>
     <span class="identifier">std</span><span class="special">::</span><span class="identifier">array</span><span class="special">&lt;</span><span class="keyword">char</span><span class="special">,</span> <span class="number">4096</span><span class="special">&gt;</span> <span class="identifier">buf</span><span class="special">;</span>

     <span class="keyword">auto</span> <span class="identifier">read</span> <span class="special">=</span> <span class="identifier">co_await</span> <span class="identifier">stream</span><span class="special">.</span><span class="identifier">async_read_some</span><span class="special">(</span>
         <span class="identifier">boost</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">buffer</span><span class="special">(</span><span class="identifier">buf</span><span class="special">),</span> <span class="identifier">deferred</span><span class="special">);</span>

     <span class="keyword">if</span> <span class="special">(</span><span class="identifier">read</span> <span class="special">==</span> <span class="number">0u</span><span class="special">)</span>
       <span class="keyword">continue</span><span class="special">;</span>

     <span class="identifier">co_yield</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string_view</span> <span class="special">{</span> <span class="identifier">buf</span><span class="special">.</span><span class="identifier">data</span><span class="special">(),</span> <span class="identifier">read</span> <span class="special">};</span>
   <span class="special">}</span>
<span class="special">}</span>

<span class="identifier">coro</span><span class="special">&lt;</span><span class="keyword">void</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span><span class="special">&gt;</span> <span class="identifier">line_logger</span><span class="special">(</span><span class="identifier">tcp</span><span class="special">::</span><span class="identifier">socket</span> <span class="identifier">stream</span><span class="special">)</span>
<span class="special">{</span>
  <span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> <span class="identifier">lines_read</span> <span class="special">=</span> <span class="number">0u</span><span class="special">;</span>
  <span class="keyword">auto</span> <span class="identifier">reader</span> <span class="special">=</span> <span class="identifier">line_reader</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">move</span><span class="special">(</span><span class="identifier">stream</span><span class="special">));</span>
  <span class="keyword">while</span> <span class="special">(</span><span class="keyword">auto</span> <span class="identifier">l</span> <span class="special">=</span> <span class="identifier">co_await</span> <span class="identifier">reader</span><span class="special">)</span>
  <span class="special">{</span>
    <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"Read: '"</span> <span class="special">&lt;&lt;</span> <span class="special">*</span><span class="identifier">l</span> <span class="special">&lt;&lt;</span> <span class="string">"'"</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
    <span class="identifier">lines_read</span><span class="special">++;</span>
  <span class="special">}</span>
  <span class="identifier">co_return</span> <span class="identifier">lines_read</span><span class="special">;</span>
<span class="special">}</span>

<span class="keyword">void</span> <span class="identifier">read_lines</span><span class="special">(</span><span class="identifier">tcp</span><span class="special">::</span><span class="identifier">socket</span> <span class="identifier">sock</span><span class="special">)</span>
<span class="special">{</span>
  <span class="identifier">co_spawn</span><span class="special">(</span><span class="identifier">line_logger</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">move</span><span class="special">(</span><span class="identifier">sock</span><span class="special">),</span>
      <span class="special">[](</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">exception_ptr</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> <span class="identifier">lines</span><span class="special">)</span>
      <span class="special">{</span>
        <span class="identifier">std</span><span class="special">::</span><span class="identifier">clog</span> <span class="special">&lt;&lt;</span> <span class="string">"Read "</span> <span class="special">&lt;&lt;</span> <span class="identifier">lines</span> <span class="special">&lt;&lt;</span> <span class="string">" lines"</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
      <span class="special">}));</span>
<span class="special">}</span>
</pre>
<p>
          A <a class="link" href="../../reference/experimental__coro.html" title="experimental::coro"><code class="computeroutput"><span class="identifier">coro</span></code></a> is highly configurable, so
          that it can cover a set of different use cases.
        </p>
<pre class="programlisting"><span class="keyword">template</span><span class="special">&lt;</span>
    <span class="keyword">typename</span> <span class="identifier">Yield</span><span class="special">,</span>
    <span class="keyword">typename</span> <span class="identifier">Return</span> <span class="special">=</span> <span class="keyword">void</span><span class="special">,</span>
    <span class="keyword">typename</span> <span class="identifier">Executor</span> <span class="special">=</span> <span class="identifier">any_io_executor</span><span class="special">&gt;</span>
<span class="keyword">struct</span> <span class="identifier">coro</span><span class="special">;</span>
</pre>
<h6>
<a name="boost_asio.overview.composition.coro.h0"></a>
          <span class="phrase"><a name="boost_asio.overview.composition.coro.yield"></a></span><a class="link" href="coro.html#boost_asio.overview.composition.coro.yield">Yield</a>
        </h6>
<p>
          The <code class="computeroutput"><span class="identifier">Yield</span></code> parameter designates
          how a <code class="computeroutput"><span class="identifier">co_yield</span></code> statement
          behaves. It can either be a type, like <code class="computeroutput"><span class="keyword">int</span></code>
          or a signature with zero or one types:
        </p>
<pre class="programlisting"><span class="identifier">coro</span><span class="special">&lt;</span><span class="keyword">void</span><span class="special">&gt;</span> <span class="comment">// A coroutine with no yield</span>
<span class="identifier">coro</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">&gt;</span> <span class="comment">// A coroutine that can yield int</span>

<span class="identifier">coro</span><span class="special">&lt;</span><span class="keyword">void</span><span class="special">()&gt;</span> <span class="comment">// A coroutine with no yield</span>
<span class="identifier">coro</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">()&gt;</span> <span class="comment">// A coroutine that can yield int</span>

<span class="identifier">coro</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">(</span><span class="keyword">double</span><span class="special">)&gt;</span> <span class="comment">// A coroutine that can yield int and receive double</span>
</pre>
<p>
          Receiving a value means that the <code class="computeroutput"><span class="identifier">co_yield</span></code>
          statement returns a value.
        </p>
<pre class="programlisting"><span class="identifier">coro</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">(</span><span class="keyword">int</span><span class="special">)&gt;</span> <span class="identifier">my_sum</span><span class="special">(</span><span class="identifier">any_io_executor</span><span class="special">)</span>
<span class="special">{</span>
  <span class="keyword">int</span> <span class="identifier">value</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span>
  <span class="keyword">while</span> <span class="special">(</span><span class="keyword">true</span><span class="special">)</span>
    <span class="identifier">value</span> <span class="special">+=</span> <span class="identifier">co_yield</span> <span class="identifier">value</span><span class="special">;</span> <span class="comment">//sum up all values</span>
<span class="special">}</span>
</pre>
<p>
          Putting values into a coroutine can be done it two ways: either by direct
          resumption (from another coro) or through async_resume. The first value
          gets ignored because the coroutines are lazy.
        </p>
<pre class="programlisting"><span class="identifier">coro</span><span class="special">&lt;</span><span class="keyword">void</span><span class="special">&gt;</span> <span class="identifier">c</span><span class="special">(</span><span class="identifier">any_io_executor</span> <span class="identifier">exec</span><span class="special">)</span>
<span class="special">{</span>
  <span class="keyword">auto</span> <span class="identifier">sum</span> <span class="special">=</span> <span class="identifier">my_sum</span><span class="special">(</span><span class="identifier">exec</span><span class="special">);</span>
  <span class="identifier">assert</span><span class="special">(</span><span class="number">0</span>  <span class="special">==</span> <span class="identifier">co_await</span> <span class="identifier">sum</span><span class="special">(-</span><span class="number">1</span><span class="special">));</span>
  <span class="identifier">assert</span><span class="special">(</span><span class="number">0</span>  <span class="special">==</span> <span class="identifier">co_await</span> <span class="identifier">sum</span><span class="special">(</span><span class="number">10</span><span class="special">));</span>
  <span class="identifier">assert</span><span class="special">(</span><span class="number">10</span> <span class="special">==</span> <span class="identifier">co_await</span> <span class="identifier">sum</span><span class="special">(</span><span class="number">15</span><span class="special">));</span>
  <span class="identifier">assert</span><span class="special">(</span><span class="number">25</span> <span class="special">==</span> <span class="identifier">co_await</span> <span class="identifier">sum</span><span class="special">(</span><span class="number">0</span><span class="special">));</span>
<span class="special">}</span>

<span class="identifier">awaitable</span><span class="special">&lt;</span><span class="keyword">void</span><span class="special">&gt;</span> <span class="identifier">a</span><span class="special">()</span>
<span class="special">{</span>
  <span class="keyword">auto</span> <span class="identifier">sum</span> <span class="special">=</span> <span class="identifier">my_sum</span><span class="special">(</span><span class="identifier">co_await</span> <span class="identifier">this_coro</span><span class="special">::</span><span class="identifier">executor</span><span class="special">);</span>
  <span class="identifier">assert</span><span class="special">(</span><span class="number">0</span>  <span class="special">==</span> <span class="identifier">co_await</span> <span class="identifier">sum</span><span class="special">.</span><span class="identifier">async_resume</span><span class="special">(-</span><span class="number">1</span><span class="special">,</span> <span class="identifier">use_awaitable</span><span class="special">));</span>
  <span class="identifier">assert</span><span class="special">(</span><span class="number">0</span>  <span class="special">==</span> <span class="identifier">co_await</span> <span class="identifier">sum</span><span class="special">.</span><span class="identifier">async_resume</span><span class="special">(</span><span class="number">10</span><span class="special">,</span> <span class="identifier">use_awaitable</span><span class="special">));</span>
  <span class="identifier">assert</span><span class="special">(</span><span class="number">10</span> <span class="special">==</span> <span class="identifier">co_await</span> <span class="identifier">sum</span><span class="special">.</span><span class="identifier">async_resume</span><span class="special">(</span><span class="number">15</span><span class="special">,</span> <span class="identifier">use_awaitable</span><span class="special">));</span>
  <span class="identifier">assert</span><span class="special">(</span><span class="number">25</span> <span class="special">==</span> <span class="identifier">co_await</span> <span class="identifier">sum</span><span class="special">.</span><span class="identifier">async_resume</span><span class="special">(</span><span class="number">0</span><span class="special">,</span> <span class="identifier">use_awaitable</span><span class="special">));</span>
<span class="special">}</span>
</pre>
<h6>
<a name="boost_asio.overview.composition.coro.h1"></a>
          <span class="phrase"><a name="boost_asio.overview.composition.coro._code__phrase_role__keyword__noexcept__phrase___code_"></a></span><a class="link" href="coro.html#boost_asio.overview.composition.coro._code__phrase_role__keyword__noexcept__phrase___code_"><code class="computeroutput"><span class="keyword">noexcept</span></code></a>
        </h6>
<p>
          A coro may be noexcept:
        </p>
<pre class="programlisting"><span class="identifier">coro</span><span class="special">&lt;</span><span class="keyword">void</span><span class="special">()</span> <span class="keyword">noexcept</span><span class="special">&gt;</span> <span class="identifier">c</span><span class="special">;</span>
<span class="identifier">coro</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">()</span> <span class="keyword">noexcept</span><span class="special">&gt;</span> <span class="identifier">c</span><span class="special">;</span>
<span class="identifier">coro</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">(</span><span class="keyword">double</span><span class="special">)</span> <span class="keyword">noexcept</span><span class="special">&gt;</span> <span class="identifier">c</span><span class="special">;</span>
</pre>
<p>
          This will change its @c async_resume signature, from <code class="computeroutput"><span class="keyword">void</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">exception_ptr</span><span class="special">)</span></code> to <code class="computeroutput"><span class="keyword">void</span><span class="special">()</span></code> or <code class="computeroutput"><span class="keyword">void</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">exception_ptr</span><span class="special">,</span> <span class="identifier">T</span><span class="special">)</span></code>
          to <code class="computeroutput"><span class="keyword">void</span><span class="special">(</span><span class="identifier">T</span><span class="special">)</span></code>. A
          noexcept coro that ends with an exception will cause <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">terminate</span></code>
          to be called.
        </p>
<p>
          Furthermore, calls of <code class="computeroutput"><span class="identifier">async_resume</span></code>
          and <code class="computeroutput"><span class="identifier">co_await</span></code> of an expired
          noexcept coro will cause undefined behaviour.
        </p>
<h6>
<a name="boost_asio.overview.composition.coro.h2"></a>
          <span class="phrase"><a name="boost_asio.overview.composition.coro.return"></a></span><a class="link" href="coro.html#boost_asio.overview.composition.coro.return">Return</a>
        </h6>
<p>
          A coro can also define a type that can be used with <code class="computeroutput"><span class="identifier">co_return</span></code>:
        </p>
<pre class="programlisting"><span class="identifier">coro</span><span class="special">&lt;</span><span class="keyword">void</span><span class="special">()</span> <span class="keyword">noexcept</span><span class="special">,</span> <span class="keyword">int</span><span class="special">&gt;</span> <span class="identifier">c</span><span class="special">(</span><span class="identifier">any_io_executor</span><span class="special">)</span>
<span class="special">{</span>
  <span class="identifier">co_return</span> <span class="number">42</span><span class="special">;</span>
<span class="special">}</span>
</pre>
<p>
          A coro can have both a <code class="computeroutput"><span class="identifier">Yield</span></code>
          and <code class="computeroutput"><span class="identifier">Return</span></code> that are non
          void at the same time.
        </p>
<h6>
<a name="boost_asio.overview.composition.coro.h3"></a>
          <span class="phrase"><a name="boost_asio.overview.composition.coro.result"></a></span><a class="link" href="coro.html#boost_asio.overview.composition.coro.result">Result</a>
        </h6>
<p>
          The result type of a coroutine is dermined by both <code class="computeroutput"><span class="identifier">Yield</span></code>
          and <code class="computeroutput"><span class="identifier">Return</span></code>. Note that in
          the follwing table only the yield output value is considered, i.e. <code class="computeroutput"><span class="identifier">T</span><span class="special">(</span><span class="identifier">U</span><span class="special">)</span></code> means <code class="computeroutput"><span class="identifier">T</span></code>.
        </p>
<div class="table">
<a name="boost_asio.overview.composition.coro.result_type"></a><p class="title"><b>Table 2. Result type deduction</b></p>
<div class="table-contents"><table class="table" summary="Result type deduction">
<colgroup>
<col>
<col>
<col>
<col>
<col>
</colgroup>
<thead><tr>
<th>
                  <p>
                    Yield
                  </p>
                </th>
<th>
                  <p>
                    Return
                  </p>
                </th>
<th>
                  <p>
                    <code class="computeroutput"><span class="keyword">noexcept</span></code>
                  </p>
                </th>
<th>
                  <p>
                    <code class="computeroutput"><span class="identifier">result_type</span></code>
                  </p>
                </th>
<th>
                  <p>
                    <code class="computeroutput"><span class="identifier">completion_signature</span></code>
                  </p>
                </th>
</tr></thead>
<tbody>
<tr>
<td>
                  <p>
                    <code class="computeroutput"><span class="identifier">T</span></code>
                  </p>
                </td>
<td>
                  <p>
                    <code class="computeroutput"><span class="identifier">U</span></code>
                  </p>
                </td>
<td>
                  <p>
                    <code class="computeroutput"><span class="keyword">false</span></code>
                  </p>
                </td>
<td>
                  <p>
                    <code class="computeroutput"><span class="identifier">variant</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">,</span>
                    <span class="identifier">U</span><span class="special">&gt;</span></code>
                  </p>
                </td>
<td>
                  <p>
                    <code class="computeroutput"><span class="keyword">void</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">exception_ptr</span><span class="special">,</span>
                    <span class="identifier">variant</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">,</span>
                    <span class="identifier">U</span><span class="special">&gt;)</span></code>
                  </p>
                </td>
</tr>
<tr>
<td>
                  <p>
                    <code class="computeroutput"><span class="identifier">T</span></code>
                  </p>
                </td>
<td>
                  <p>
                    <code class="computeroutput"><span class="identifier">U</span></code>
                  </p>
                </td>
<td>
                  <p>
                    <code class="computeroutput"><span class="keyword">true</span></code>
                  </p>
                </td>
<td>
                  <p>
                    <code class="computeroutput"><span class="identifier">variant</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">,</span>
                    <span class="identifier">U</span><span class="special">&gt;</span></code>
                  </p>
                </td>
<td>
                  <p>
                    <code class="computeroutput"><span class="keyword">void</span><span class="special">(</span><span class="identifier">variant</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">,</span>
                    <span class="identifier">U</span><span class="special">&gt;)</span></code>
                  </p>
                </td>
</tr>
<tr>
<td>
                  <p>
                    <code class="computeroutput"><span class="identifier">T</span></code>
                  </p>
                </td>
<td>
                  <p>
                    <code class="computeroutput"><span class="keyword">void</span></code>
                  </p>
                </td>
<td>
                  <p>
                    <code class="computeroutput"><span class="keyword">false</span></code>
                  </p>
                </td>
<td>
                  <p>
                    <code class="computeroutput"><span class="identifier">optional</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;</span></code>
                  </p>
                </td>
<td>
                  <p>
                    <code class="computeroutput"><span class="keyword">void</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">exception_ptr</span><span class="special">,</span>
                    <span class="identifier">optional</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;)</span></code>
                  </p>
                </td>
</tr>
<tr>
<td>
                  <p>
                    <code class="computeroutput"><span class="identifier">T</span></code>
                  </p>
                </td>
<td>
                  <p>
                    <code class="computeroutput"><span class="keyword">void</span></code>
                  </p>
                </td>
<td>
                  <p>
                    <code class="computeroutput"><span class="keyword">true</span></code>
                  </p>
                </td>
<td>
                  <p>
                    <code class="computeroutput"><span class="identifier">optional</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;</span></code>
                  </p>
                </td>
<td>
                  <p>
                    <code class="computeroutput"><span class="keyword">void</span><span class="special">(</span><span class="identifier">optional</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;)</span></code>
                  </p>
                </td>
</tr>
<tr>
<td>
                  <p>
                    <code class="computeroutput"><span class="keyword">void</span></code>
                  </p>
                </td>
<td>
                  <p>
                    <code class="computeroutput"><span class="keyword">void</span></code>
                  </p>
                </td>
<td>
                  <p>
                    <code class="computeroutput"><span class="keyword">false</span></code>
                  </p>
                </td>
<td>
                  <p>
                    <code class="computeroutput"><span class="identifier">optional</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;</span></code>
                  </p>
                </td>
<td>
                  <p>
                    <code class="computeroutput"><span class="keyword">void</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">exception_ptr</span><span class="special">)</span></code>
                  </p>
                </td>
</tr>
<tr>
<td>
                  <p>
                    <code class="computeroutput"><span class="keyword">void</span></code>
                  </p>
                </td>
<td>
                  <p>
                    <code class="computeroutput"><span class="keyword">void</span></code>
                  </p>
                </td>
<td>
                  <p>
                    <code class="computeroutput"><span class="keyword">true</span></code>
                  </p>
                </td>
<td>
                  <p>
                    <code class="computeroutput"><span class="identifier">optional</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;</span></code>
                  </p>
                </td>
<td>
                  <p>
                    <code class="computeroutput"><span class="keyword">void</span><span class="special">()</span></code>
                  </p>
                </td>
</tr>
<tr>
<td>
                  <p>
                    <code class="computeroutput"><span class="keyword">void</span></code>
                  </p>
                </td>
<td>
                  <p>
                    <code class="computeroutput"><span class="identifier">T</span></code>
                  </p>
                </td>
<td>
                  <p>
                    <code class="computeroutput"><span class="keyword">false</span></code>
                  </p>
                </td>
<td>
                  <p>
                    <code class="computeroutput"><span class="identifier">optional</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;</span></code>
                  </p>
                </td>
<td>
                  <p>
                    <code class="computeroutput"><span class="keyword">void</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">exception_ptr</span><span class="special">,</span>
                    <span class="identifier">T</span><span class="special">)</span></code>
                  </p>
                </td>
</tr>
<tr>
<td>
                  <p>
                    <code class="computeroutput"><span class="keyword">void</span></code>
                  </p>
                </td>
<td>
                  <p>
                    <code class="computeroutput"><span class="identifier">T</span></code>
                  </p>
                </td>
<td>
                  <p>
                    <code class="computeroutput"><span class="keyword">true</span></code>
                  </p>
                </td>
<td>
                  <p>
                    <code class="computeroutput"><span class="identifier">optional</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;</span></code>
                  </p>
                </td>
<td>
                  <p>
                    <code class="computeroutput"><span class="keyword">void</span><span class="special">(</span><span class="identifier">T</span><span class="special">)</span></code>
                  </p>
                </td>
</tr>
</tbody>
</table></div>
</div>
<br class="table-break"><h6>
<a name="boost_asio.overview.composition.coro.h4"></a>
          <span class="phrase"><a name="boost_asio.overview.composition.coro.executor"></a></span><a class="link" href="coro.html#boost_asio.overview.composition.coro.executor">Executor</a>
        </h6>
<p>
          Every coroutine needs to have its own executor. Since the coroutine gets
          called multiple times, it cannot take the executor from the caller like
          an <code class="computeroutput"><span class="identifier">awaitable</span></code>. Therefore
          a <code class="computeroutput"><span class="identifier">coro</span></code> requires to get
          an executor or an execution_context passed in as the first parameter.
        </p>
<pre class="programlisting"><span class="identifier">coro</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">&gt;</span> <span class="identifier">with_executor</span><span class="special">(</span><span class="identifier">any_io_executor</span><span class="special">);</span>
<span class="identifier">coro</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">&gt;</span> <span class="identifier">with_context</span><span class="special">(</span><span class="identifier">io_context</span> <span class="special">&amp;);</span>
</pre>
<p>
          It is to note, that an execution_context is defined as loosely as possible.
          An execution context is any object that has a <code class="computeroutput"><span class="identifier">get_executor</span><span class="special">()</span></code> function, which returns an executor that
          can be transformed into the executor_type of the coroutine. This allows
          most io_objects to be used as the source of the executor:
        </p>
<pre class="programlisting"><span class="identifier">coro</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">&gt;</span> <span class="identifier">with_socket</span><span class="special">(</span><span class="identifier">tcp</span><span class="special">::</span><span class="identifier">socket</span><span class="special">);</span>
</pre>
<p>
          Additionally, a <code class="computeroutput"><span class="identifier">coro</span></code> that
          is a member function will check the <code class="computeroutput"><span class="keyword">this</span></code>
          pointer as well, either if it's an executor or an execution context:
        </p>
<pre class="programlisting"><span class="keyword">struct</span> <span class="identifier">my_io_object</span>
<span class="special">{</span>
  <span class="identifier">any_io_executor</span> <span class="identifier">get_executor</span><span class="special">();</span>

  <span class="identifier">coro</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">&gt;</span> <span class="identifier">my_coro</span><span class="special">();</span>
<span class="special">};</span>
</pre>
<p>
          Finally, a member coro can be given an explicit executor or execution context,
          to override the one of the object:
        </p>
<pre class="programlisting"><span class="keyword">struct</span> <span class="identifier">my_io_object</span>
<span class="special">{</span>
  <span class="identifier">any_io_executor</span> <span class="identifier">get_executor</span><span class="special">();</span>

  <span class="identifier">coro</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">&gt;</span> <span class="identifier">my_coro</span><span class="special">(</span><span class="identifier">any_io_executor</span> <span class="identifier">exec</span><span class="special">);</span> <span class="comment">// it will use exec</span>
<span class="special">};</span>
</pre>
<h6>
<a name="boost_asio.overview.composition.coro.h5"></a>
          <span class="phrase"><a name="boost_asio.overview.composition.coro._code__phrase_role__identifier__co_await__phrase___code_"></a></span><a class="link" href="coro.html#boost_asio.overview.composition.coro._code__phrase_role__identifier__co_await__phrase___code_"><code class="computeroutput"><span class="identifier">co_await</span></code></a>
        </h6>
<p>
          The @c co_await within a <code class="computeroutput"><span class="identifier">coro</span></code>
          is not the same as <code class="computeroutput"><span class="identifier">async_resume</span><span class="special">(</span><span class="identifier">deferred</span><span class="special">)</span></code>, unless both coros use different executors.
          If they use the same, the <code class="computeroutput"><span class="identifier">coro</span></code>
          will direclty suspend and resume the executor, without any usage of the
          executor.
        </p>
<p>
          <code class="computeroutput"><span class="identifier">co_await</span> <span class="identifier">this_coro</span><span class="special">::</span></code> behaves the same as coroutines that use
          @c boost::asio::awaitable.
        </p>
<h6>
<a name="boost_asio.overview.composition.coro.h6"></a>
          <span class="phrase"><a name="boost_asio.overview.composition.coro.integrating_with_awaitable"></a></span><a class="link" href="coro.html#boost_asio.overview.composition.coro.integrating_with_awaitable">Integrating
          with awaitable</a>
        </h6>
<p>
          As the <code class="computeroutput"><span class="identifier">coro</span></code> member function
          <code class="computeroutput"><span class="identifier">async_resume</span></code> is an asynchronous
          operation, it may also be used in conjunction with <code class="computeroutput"><span class="identifier">awaitable</span></code>
          coroutines in a single control flow. For example:
        </p>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">asio</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">asio</span><span class="special">/</span><span class="identifier">experimental</span><span class="special">/</span><span class="identifier">coro</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>

<span class="keyword">using</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">ip</span><span class="special">::</span><span class="identifier">tcp</span><span class="special">;</span>

<span class="identifier">boost</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">experimental</span><span class="special">::</span><span class="identifier">coro</span><span class="special">&lt;</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">&gt;</span> <span class="identifier">reader</span><span class="special">(</span><span class="identifier">tcp</span><span class="special">::</span><span class="identifier">socket</span><span class="special">&amp;</span> <span class="identifier">sock</span><span class="special">)</span>
<span class="special">{</span>
  <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">buf</span><span class="special">;</span>
  <span class="keyword">while</span> <span class="special">(</span><span class="identifier">sock</span><span class="special">.</span><span class="identifier">is_open</span><span class="special">())</span>
  <span class="special">{</span>
    <span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> <span class="identifier">n</span> <span class="special">=</span> <span class="identifier">co_await</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">async_read_until</span><span class="special">(</span>
        <span class="identifier">sock</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">dynamic_buffer</span><span class="special">(</span><span class="identifier">buf</span><span class="special">),</span> <span class="char">'\n'</span><span class="special">,</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">deferred</span><span class="special">);</span>
    <span class="identifier">co_yield</span> <span class="identifier">buf</span><span class="special">.</span><span class="identifier">substr</span><span class="special">(</span><span class="number">0</span><span class="special">,</span> <span class="identifier">n</span><span class="special">);</span>
    <span class="identifier">buf</span><span class="special">.</span><span class="identifier">erase</span><span class="special">(</span><span class="number">0</span><span class="special">,</span> <span class="identifier">n</span><span class="special">);</span>
  <span class="special">}</span>
<span class="special">}</span>

<span class="identifier">boost</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">awaitable</span><span class="special">&lt;</span><span class="keyword">void</span><span class="special">&gt;</span> <span class="identifier">consumer</span><span class="special">(</span><span class="identifier">tcp</span><span class="special">::</span><span class="identifier">socket</span> <span class="identifier">sock</span><span class="special">)</span>
<span class="special">{</span>
  <span class="keyword">auto</span> <span class="identifier">r</span> <span class="special">=</span> <span class="identifier">reader</span><span class="special">(</span><span class="identifier">sock</span><span class="special">);</span>
  <span class="keyword">auto</span> <span class="identifier">msg1</span> <span class="special">=</span> <span class="identifier">co_await</span> <span class="identifier">r</span><span class="special">.</span><span class="identifier">async_resume</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">use_awaitable</span><span class="special">);</span>
  <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"Message 1: "</span> <span class="special">&lt;&lt;</span> <span class="identifier">msg1</span><span class="special">.</span><span class="identifier">value_or</span><span class="special">(</span><span class="string">"\n"</span><span class="special">);</span>
  <span class="keyword">auto</span> <span class="identifier">msg2</span> <span class="special">=</span> <span class="identifier">co_await</span> <span class="identifier">r</span><span class="special">.</span><span class="identifier">async_resume</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">use_awaitable</span><span class="special">);</span>
  <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"Message 2: "</span> <span class="special">&lt;&lt;</span> <span class="identifier">msg2</span><span class="special">.</span><span class="identifier">value_or</span><span class="special">(</span><span class="string">"\n"</span><span class="special">);</span>
<span class="special">}</span>

<span class="identifier">boost</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">awaitable</span><span class="special">&lt;</span><span class="keyword">void</span><span class="special">&gt;</span> <span class="identifier">listen</span><span class="special">(</span><span class="identifier">tcp</span><span class="special">::</span><span class="identifier">acceptor</span><span class="special">&amp;</span> <span class="identifier">acceptor</span><span class="special">)</span>
<span class="special">{</span>
  <span class="keyword">for</span> <span class="special">(;;)</span>
  <span class="special">{</span>
    <span class="identifier">co_spawn</span><span class="special">(</span>
        <span class="identifier">acceptor</span><span class="special">.</span><span class="identifier">get_executor</span><span class="special">(),</span>
        <span class="identifier">consumer</span><span class="special">(</span><span class="identifier">co_await</span> <span class="identifier">acceptor</span><span class="special">.</span><span class="identifier">async_accept</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">use_awaitable</span><span class="special">)),</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">detached</span><span class="special">);</span>
  <span class="special">}</span>
<span class="special">}</span>

<span class="keyword">int</span> <span class="identifier">main</span><span class="special">()</span>
<span class="special">{</span>
  <span class="identifier">boost</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">io_context</span> <span class="identifier">ctx</span><span class="special">;</span>
  <span class="identifier">tcp</span><span class="special">::</span><span class="identifier">acceptor</span> <span class="identifier">acceptor</span><span class="special">(</span><span class="identifier">ctx</span><span class="special">,</span> <span class="special">{</span><span class="identifier">tcp</span><span class="special">::</span><span class="identifier">v4</span><span class="special">(),</span> <span class="number">54321</span><span class="special">});</span>
  <span class="identifier">co_spawn</span><span class="special">(</span><span class="identifier">ctx</span><span class="special">,</span> <span class="identifier">listen</span><span class="special">(</span><span class="identifier">acceptor</span><span class="special">),</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">detached</span><span class="special">);</span>
  <span class="identifier">ctx</span><span class="special">.</span><span class="identifier">run</span><span class="special">();</span>
<span class="special">}</span>
</pre>
<h6>
<a name="boost_asio.overview.composition.coro.h7"></a>
          <span class="phrase"><a name="boost_asio.overview.composition.coro.see_also"></a></span><a class="link" href="coro.html#boost_asio.overview.composition.coro.see_also">See
          Also</a>
        </h6>
<p>
          <a class="link" href="../../reference/co_spawn.html" title="co_spawn">co_spawn</a>, <a class="link" href="../../reference/experimental__coro.html" title="experimental::coro">experimental::coro</a>,
          <a class="link" href="cpp20_coroutines.html" title="C++20 Coroutines Support">C++20
          Coroutines</a>, <a class="link" href="spawn.html" title="Stackful Coroutines">Stackful
          Coroutines</a>, <a class="link" href="coroutine.html" title="Stackless Coroutines">Stackless
          Coroutines</a>.
        </p>
</div>
<div class="copyright-footer">Copyright © 2003-2024 Christopher M. Kohlhoff<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
      </p>
</div>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="cpp20_coroutines.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../composition.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../boost_asio.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="deferred.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>
